package com.tseacher.web;

import com.tseacher.pojo.Link;
import com.tseacher.service.LinkService;
import com.tseacher.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/link")
public class LinkController {
    @Autowired
    private LinkService linkService;
    @RequestMapping("/to_link")
    public String toLink(){
        return "forward:/link/getPage?cpage=1&rows=5";
    }
    @RequestMapping("/addLink")
    public String addLink(Link link){
        System.out.println("新增"+link);
        linkService.add(link);
        return "forward:/link/getPage?cpage=1&rows=5";
    }
    @RequestMapping("/getPage")
    public String getPage(Integer cpage, Integer rows, HttpServletRequest request){
        cpage = cpage==null?1:cpage;
        rows = rows==null?5:rows;
        Page<Link> page = linkService.getPage(cpage,rows);
        request.setAttribute("page",page);
        return "link";
    }
    @RequestMapping("/delete")
    public String deleteLink(Integer id){
        if(id!=null){
            linkService.deleteLink(id);
        }
        return "forward:/link/getPage?cpage=1&rows=5";
    }
}
